﻿@{
    ViewBag.Title = "字典管理";
    Layout = "_Index";
}
@*<link href="~/plugins/zTree_v3/css/zTreeStyle/zTreeStyle.css" rel="stylesheet" />*@
<link href="~/plugins/zTree_v3/css/bootstrapStyle/bootstrapStyle.css" rel="stylesheet" />
<div class="ibox">
    <div class="ibox-content">
        <div class="row">
            <div class="col-sm-2">
                <div class="ibox ">
                    <ul id="dicTree" class="ztree"></ul>
                </div>
            </div>
            <div class="col-sm-10">
                <div id="toolbar" class="btn-group">

                </div>
                <table id="dicTable"></table>
            </div>
        </div>
    </div>
</div>
@section scripts
    {
    <script src="~/plugins/zTree_v3/js/jquery.ztree.all.min.js"></script>
    <script>
        var ptree, $dicTable, pid = 0;
        $(function () {
            loadDicTree();
            loadGrid();
        });

        function loadDicTree() {
            var opts = {

                data: {
                    simpleData: {
                        enable: true
                    }
                }, callback: {
                    onClick: DicZtreeClick

                }
            };
            jutils.ajaxGet('/admin/sysdic/getdicztree', null, function (res) {
                ptree = $.fn.zTree.init($("#dicTree"), opts, res.data.dicTree);
                ptree.expandAll(true);
            });
        }

        //单击事件
        function DicZtreeClick(event, treeId, treeNode) {
            pid = treeNode.id;

            $dicTable.bootstrapTable('refresh');

        }

        function loadGrid() {
            $dicTable = $("#dicTable").bootstrapTable({
                url: '/admin/sysdic/GetGridDataBypId',
                toolbar: '#toolbar',
                pagination: true,
                height: $(window).height() - 50,
                pageSize: 15,
                pageList: [5, 10, 15, 20],
                showColumns: true,
                showRefresh: true,
                clickToSelect: true,
                sidePagination: "client",//server
                locale: "zh-CN",
                striped: true,
                queryParams: function (params) {
                    var dto = {
                        limit: params.limit, //每页多少条
                        offset: params.offset, //从第几条开始读(默认就好)
                        pid: pid
                    };
                    return dto;
                },
                columns: [
                    { field: 'ck', checkbox: true },
                    { field: 'dic_name', title: '字典名称', align: 'center' },
                    { field: 'dic_code', title: '字典编码', align: 'center' },
                    { field: 'sort_num', title: '排序', align: 'center' },
                    { field: 'remark', title: '备注', align: 'center' },
                    {
                        field: 'id', title: '操作', align: 'center',
                        formatter: render
                    }
                ]

            });
            //jutils.initToolBar('#toolbar', true);
            jutils.initToolBarRole('#toolbar');
        }

        function render(v, r, i) {
            var html = jutils.initToolBarRow(v);
            return html;
        }

        function add() {
            var node = ptree.getSelectedNodes();
            if (node.length === 0) {
                jutils.warn("请先选择左侧通用字典项");
                return;
            }
            var id = node[0].id;
            jutils.dialog('字典信息', '/admin/sysdic/form', { id: '', pid: id }, null,
                function () {
                    $dicTable.bootstrapTable('refresh');
                    if (id === '0') {
                        loadDicTree();
                    }
                });
        }
        function edit(obj) {
            var id = $(obj).data('id');
            jutils.dialog('字典信息', '/admin/sysdic/form', { id: id }, null,
                function () {
                    $dicTable.bootstrapTable('refresh');
                    if (pid === '0') {
                        loadDicTree();
                    }
                });
        }

        function delSingle(obj) {
            var id = $(obj).data('id');
            jutils.confirm('确认删除吗', function () {
                jutils.ajaxGet('/admin/sysdic/del', { ids: id }, function () {
                    $dicTable.bootstrapTable('refresh');
                    loadDicTree();
                });
            });
        }
        function del() {
            var rows = $dicTable.bootstrapTable('getSelections');
            if (rows.length === 0) {
                jutils.warn('请选中需要删除的数据行');
                return;
            }

            var ids = [];
            for (var i = 0; i < rows.length; i++) {

                ids.push(rows[i].id);

            }

            jutils.confirm('确认删除吗', function () {
                jutils.ajaxGet('/admin/sysdic/del', { ids: ids.join() }, function () {
                    $dicTable.bootstrapTable('refresh');
                    loadDicTree();
                });
            });
        }
    </script>


}